package com.demo.library2.servicelmpl;

import com.demo.library2.Mapper.BookRepository;
import com.demo.library2.entity.Book;
import com.demo.library2.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookRepository bookRepository;
    @Override
    public List<Book> findAll() {
        return bookRepository.findAll();
    }
    @Override
    public Book findById(Long id) {
        return bookRepository.getById(id);
    }
    @Override
    public void save(Book book) {
        bookRepository.save(book);
    }
    @Override
    public void update(Book book) {
        bookRepository.save(book);
    }
    @Override
    public void deleteById(Long id) {
        bookRepository.deleteById(id);
    }
    @Override
    public List<Book> findByTitle(String title) {
        return bookRepository.findByTitleContains(title.trim());
    }
    @Override
    public String getPublisherNameById(Long id) {
        Book book = bookRepository.getById(id);
        if (book != null) {
            return book.getPublisher().getPublisherName();
        }
        return null;
    }
    @Override
    public List<String> findAuthorList() {
        return bookRepository.getAuthorList();
    }
}